//单链表的模拟
public class MySingleList {
    class Node{
        int val;//存储的数据
        public Node next;//下一个元素的地址
        public Node(int val) {
            this.val = val;
            usedsize++;
        }
    }
    public int usedsize;
    public Node head;
    /*public void creatlink(){
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = node1;
    }*/
    //头插法
    public void addFirst(int data){
        /*if(head == null){
            Node node = new Node(data);
        }*/
        //链表中是否存在数据不影响插入
        Node node = new Node(data);
        node.next = head;
        head = node;
        usedsize++;
    }
    //尾插法
    public void addLast(int data){
        if (head == null){
            //头结点为空 直接头插法插入数据
            addFirst(data);
            return;
        }
        Node cur = head;
        Node node = new Node(data);
        while (cur.next != null){
            cur =cur.next;
        }
        cur.next = node;
        //usedsize++;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex (int index,int data)throws SubscriptError{
        checkindex(index);
        if (head == null){
            addFirst(data);
            return;
        }
        //下标为0，头插法
        if(index == 0){
            addFirst(data);
            return;
        }
        //下标如果是最后一个元素吗，尾插法
        if(index  == size()){
            addLast(data);
            return;
        }
        Node node = new Node(data);
        Node cur = findindexsubone(index);
        node.next = cur.next;
        cur.next = node;
    }
    /*
    找到要插入下标的前一个位置的元素
     */
    private Node findindexsubone(int index){
        Node cur = head;
        int count = 0;
        while (count!=index-1){
            cur = cur.next;
        }
        return cur;
    }
    private void checkindex(int index){
        if(index > size()|| index < 0){
            throw new SubscriptError("下标不合法");
        }
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(head == null){
            return false;
        }
        Node cur = head;
        while (cur != null){
            if(cur.val == key){
                return true;
            }
            cur =cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null){
            return;
        }
        if(head.val == key){
            head = head.next;
        }
        Node cur = head;
        Node prevnode = cur;
        while (cur != null){
            if(cur.val == key){
                prevnode.next = cur.next;
                //this.usedsize--;
                return;
            }
            prevnode = cur;
            cur =cur.next;
        }
        return;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        while (head!=null&&head.val==key){
            head = head.next;
        }
        Node cur = head;
        Node prevnode = cur;
        while (cur!= null){
            if(cur.val == key){
                prevnode.next = cur.next;
                cur = cur.next;
                //this.usedsize--;
            }else{
                prevnode = cur;
                cur = cur.next;
            }
        }
        return;
    }
    //得到单链表的长度
    public int size(){

        //时间复杂度O(N)
        if (head == null){
            return 0;
        }
        int count = 0;
        Node cur = head;
        while (cur != null){
            cur =cur.next;
            count++;
        }
        return count;
        //return this.usedsize;
    }
    public void display(){
        Node cur = head;
        while (cur!= null){
            System.out.print(cur.val+" ");
            cur =cur.next;
        }
    }
    public void clear(){
        head = null;
    }
}
